package br.ufla.pgcc.oomeasures.astvisitors;

import java.util.Iterator;
import java.util.List;

import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.TypeDeclaration;

public class ClassVisitor extends ASTVisitor {
	
	private static int numOfProjectClasses;
	private int numOfClassClasses;
	
	public boolean visit(CompilationUnit unit) {
		
		calculateNumberOfClasses(unit);
		
		return false;
	}

	@SuppressWarnings("unchecked")
	private void calculateNumberOfClasses(CompilationUnit unit) {

		for (Object type :unit.types()){
			if (type instanceof TypeDeclaration){
				
				if (!((TypeDeclaration) type).isInterface()){
					
					numOfProjectClasses++;
					numOfClassClasses++;
					
					List<TypeDeclaration> bodyDeclarationsList = ((TypeDeclaration) type).bodyDeclarations();
					
					Iterator<TypeDeclaration> itBodyDeclaration = bodyDeclarationsList.iterator();
					
					while (itBodyDeclaration.hasNext()){
						Object itItem = itBodyDeclaration.next();
						if ((itItem instanceof TypeDeclaration) && (!((TypeDeclaration) itItem).isInterface())){
							numOfProjectClasses++;
							numOfClassClasses++;
						}
					}
				}
			}
		}
	}
	
	
	public static int getNumOfProjectClasses() {
		return numOfProjectClasses;
	}
	
	public int getNumOfClasses() {
		return numOfClassClasses;
	}
} 